#ifndef THREADPOOL_H
#define THREADPOOL_H
#include <iostream>
#include <thread>
#include <functional>
#include <queue>
#include <vector>
#include <atomic>
#include <mutex>
#include <condition_variable>
#include <chrono>
 
class CThreadPool
{
    using Task = std::function<void()>; //函数包装模板
private:
    CThreadPool();
    ~CThreadPool();
    CThreadPool(const CThreadPool&) = delete;
    CThreadPool& operator=(const CThreadPool&) = delete;
public:
    static CThreadPool& GetInstance();
 
    void SetMaxThreadNum(const int iCount);
 
    void OnStart();
 
    void OnStop();
 
    void AppendTask(const Task& _task);
 
private:
    void DoWork();
 
private:
    std::atomic_bool m_bIsRunning{false};
    unsigned short m_usThreadNum{2};
    std::vector<std::thread> m_vctThreads;
    std::queue<Task> m_queTasks;
    std::condition_variable m_cv;
    std::mutex m_mtx;
};
 
 
 
#endif /* THREADPOOL_H */